package com.tong.calculator.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.tong.calculator.common.PageQuery;
import com.tong.calculator.response.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import com.tong.calculator.pojo.DO.QuestionDO;
import com.tong.calculator.service.QuestionService;


/**
 * @author tongjiaqing
 * @email tongjiaqing@mail.com
 * @date 2022-03-20 18:40:29
 */
@CrossOrigin(origins = "*")
@Api(tags = "历史计算")
@Controller
@AllArgsConstructor
@RequestMapping("/question")
public class QuestionController {

    private final QuestionService questionService;

    /**
     * 首页
     */
    @ApiOperation("首页")
    @ApiOperationSupport(order = -1)
    @GetMapping("/index")
    public String index(Model model) {
        return "index";
    }

    /**
     * 列表
     */
    @ApiOperation("历史")
    @ApiOperationSupport(order = 0)
    @GetMapping("/list")
    public String list(Model model) {
        List<QuestionDO> list = questionService.list();
        model.addAttribute("resultList", list);

        return "list";
    }

    /**
     * 分页
     */
    @ApiOperation("分页")
    @ApiOperationSupport(order = 1)
    @GetMapping("/page")
    @ResponseBody
    public R page() {
        QuestionDO question = questionService.getById(1);

        return R.success(question);
    }


    /**
     * 信息
     */
    @ApiOperation("信息")
    @ApiOperationSupport(order = 2)
    @GetMapping("/info/{id}")
    @ResponseBody
    public R info(@PathVariable("id") Integer id) {
        QuestionDO question = questionService.getById(id);

        return R.success(question);
    }

    /**
     * 保存
     */
    @ApiOperation("保存")
    @ApiOperationSupport(order = 3)
    @PostMapping("/save")
    @ResponseBody
    public R save(@RequestBody QuestionDO question) {
        return R.build(questionService.save(question));
    }

    /**
     * 修改
     */
    @ApiOperation("修改")
    @ApiOperationSupport(order = 4)
    @PutMapping("/update")
    @ResponseBody
    public R update(@RequestBody QuestionDO question) {
        return R.build(questionService.updateById(question));
    }

    /**
     * 删除
     */
    @ApiOperation("删除")
    @ApiOperationSupport(order = 5)
    @GetMapping("/delete/{id}")
    public String delete(@PathVariable("id") Integer id) {
        questionService.removeById(id);

        return "redirect:/question/list";
    }

}
